{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b49cce40-cee0-4e6f-b61b-5c15c70712a0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "026c33a5-a046-4e27-8cc9-1d18e69547f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from datetime import timedelta\n",
    "\n",
    "# ========== 数据准备 ==========\n",
    "# 模拟1000辆商用车的行驶记录（每辆车30天数据）\n",
    "np.random.seed(42)\n",
    "vehicle_ids = np.arange(1000)\n",
    "dates = pd.date_range(\"2023-01-01\", \"2023-01-30\")\n",
    "\n",
    "# 创建模拟数据集\n",
    "data = pd.DataFrame({\n",
    "    \"vehicle_id\": np.random.choice(vehicle_ids, size=50000),\n",
    "    \"timestamp\": np.random.choice(dates, size=50000),\n",
    "    \"speed\": np.clip(np.random.normal(70, 25, 50000), 0, 120),  # 车速(km/h)\n",
    "    \"acceleration\": np.random.normal(0, 2, 50000),  # 加速度(m/s²)\n",
    "    \"braking_force\": np.random.uniform(0, 1, 50000),  # 刹车力度(0-1)\n",
    "    \"steering_angle\": np.random.normal(0, 15, 50000),  # 方向盘转角(度)\n",
    "    \"continuous_driving\": np.random.exponential(4, 50000),  # 连续驾驶时长(小时)\n",
    "    \"location\": np.random.choice([\"高速\", \"国道\", \"市区\"], 50000),\n",
    "    \"night_driving\": np.random.choice([0, 1], 50000, p=[0.7, 0.3])  # 是否夜间驾驶\n",
    "})\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "536f268d-eff8-40f2-ae71-13023bf58621",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>vehicle_id</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>speed</th>\n",
       "      <th>acceleration</th>\n",
       "      <th>braking_force</th>\n",
       "      <th>steering_angle</th>\n",
       "      <th>continuous_driving</th>\n",
       "      <th>location</th>\n",
       "      <th>night_driving</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>102</td>\n",
       "      <td>2023-01-01</td>\n",
       "      <td>75.472060</td>\n",
       "      <td>-0.465906</td>\n",
       "      <td>0.869633</td>\n",
       "      <td>11.094222</td>\n",
       "      <td>1.628275</td>\n",
       "      <td>市区</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>435</td>\n",
       "      <td>2023-01-27</td>\n",
       "      <td>63.045652</td>\n",
       "      <td>-2.997739</td>\n",
       "      <td>0.261838</td>\n",
       "      <td>7.092074</td>\n",
       "      <td>8.379536</td>\n",
       "      <td>高速</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>860</td>\n",
       "      <td>2023-01-12</td>\n",
       "      <td>73.134776</td>\n",
       "      <td>1.668533</td>\n",
       "      <td>0.561717</td>\n",
       "      <td>-14.696779</td>\n",
       "      <td>18.049646</td>\n",
       "      <td>高速</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>270</td>\n",
       "      <td>2023-01-04</td>\n",
       "      <td>89.514422</td>\n",
       "      <td>-1.325766</td>\n",
       "      <td>0.699275</td>\n",
       "      <td>-18.843614</td>\n",
       "      <td>0.988950</td>\n",
       "      <td>市区</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>106</td>\n",
       "      <td>2023-01-30</td>\n",
       "      <td>64.221220</td>\n",
       "      <td>5.614003</td>\n",
       "      <td>0.299589</td>\n",
       "      <td>15.186566</td>\n",
       "      <td>17.039108</td>\n",
       "      <td>高速</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49995</th>\n",
       "      <td>381</td>\n",
       "      <td>2023-01-15</td>\n",
       "      <td>18.177682</td>\n",
       "      <td>-3.431543</td>\n",
       "      <td>0.591230</td>\n",
       "      <td>-14.562768</td>\n",
       "      <td>1.046809</td>\n",
       "      <td>高速</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49996</th>\n",
       "      <td>44</td>\n",
       "      <td>2023-01-07</td>\n",
       "      <td>120.000000</td>\n",
       "      <td>1.958547</td>\n",
       "      <td>0.861104</td>\n",
       "      <td>1.955887</td>\n",
       "      <td>0.141749</td>\n",
       "      <td>国道</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49997</th>\n",
       "      <td>916</td>\n",
       "      <td>2023-01-24</td>\n",
       "      <td>49.901630</td>\n",
       "      <td>0.306843</td>\n",
       "      <td>0.146259</td>\n",
       "      <td>-1.815499</td>\n",
       "      <td>2.017120</td>\n",
       "      <td>国道</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49998</th>\n",
       "      <td>171</td>\n",
       "      <td>2023-01-27</td>\n",
       "      <td>45.385097</td>\n",
       "      <td>-2.429129</td>\n",
       "      <td>0.370914</td>\n",
       "      <td>-5.591587</td>\n",
       "      <td>3.412434</td>\n",
       "      <td>高速</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49999</th>\n",
       "      <td>220</td>\n",
       "      <td>2023-01-02</td>\n",
       "      <td>81.593714</td>\n",
       "      <td>-0.612339</td>\n",
       "      <td>0.556499</td>\n",
       "      <td>7.657142</td>\n",
       "      <td>10.762482</td>\n",
       "      <td>市区</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>50000 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       vehicle_id  timestamp       speed  acceleration  braking_force  \\\n",
       "0             102 2023-01-01   75.472060     -0.465906       0.869633   \n",
       "1             435 2023-01-27   63.045652     -2.997739       0.261838   \n",
       "2             860 2023-01-12   73.134776      1.668533       0.561717   \n",
       "3             270 2023-01-04   89.514422     -1.325766       0.699275   \n",
       "4             106 2023-01-30   64.221220      5.614003       0.299589   \n",
       "...           ...        ...         ...           ...            ...   \n",
       "49995         381 2023-01-15   18.177682     -3.431543       0.591230   \n",
       "49996          44 2023-01-07  120.000000      1.958547       0.861104   \n",
       "49997         916 2023-01-24   49.901630      0.306843       0.146259   \n",
       "49998         171 2023-01-27   45.385097     -2.429129       0.370914   \n",
       "49999         220 2023-01-02   81.593714     -0.612339       0.556499   \n",
       "\n",
       "       steering_angle  continuous_driving location  night_driving  \n",
       "0           11.094222            1.628275       市区              1  \n",
       "1            7.092074            8.379536       高速              1  \n",
       "2          -14.696779           18.049646       高速              0  \n",
       "3          -18.843614            0.988950       市区              0  \n",
       "4           15.186566           17.039108       高速              1  \n",
       "...               ...                 ...      ...            ...  \n",
       "49995      -14.562768            1.046809       高速              0  \n",
       "49996        1.955887            0.141749       国道              0  \n",
       "49997       -1.815499            2.017120       国道              1  \n",
       "49998       -5.591587            3.412434       高速              0  \n",
       "49999        7.657142           10.762482       市区              0  \n",
       "\n",
       "[50000 rows x 9 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cc99046b-800b-4684-b1f6-9c3c255d43d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 添加时间维度\n",
    "data[\"hour\"] = np.random.randint(0, 24, 50000)\n",
    "data[\"is_night\"] = data[\"hour\"].apply(lambda x: 1 if x < 6 or x > 22 else 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e6255337-5ac9-4eac-ba43-24e84028696d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>vehicle_id</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>speed</th>\n",
       "      <th>acceleration</th>\n",
       "      <th>braking_force</th>\n",
       "      <th>steering_angle</th>\n",
       "      <th>continuous_driving</th>\n",
       "      <th>location</th>\n",
       "      <th>night_driving</th>\n",
       "      <th>hour</th>\n",
       "      <th>is_night</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>102</td>\n",
       "      <td>2023-01-01</td>\n",
       "      <td>75.472060</td>\n",
       "      <td>-0.465906</td>\n",
       "      <td>0.869633</td>\n",
       "      <td>11.094222</td>\n",
       "      <td>1.628275</td>\n",
       "      <td>市区</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>435</td>\n",
       "      <td>2023-01-27</td>\n",
       "      <td>63.045652</td>\n",
       "      <td>-2.997739</td>\n",
       "      <td>0.261838</td>\n",
       "      <td>7.092074</td>\n",
       "      <td>8.379536</td>\n",
       "      <td>高速</td>\n",
       "      <td>1</td>\n",
       "      <td>13</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>860</td>\n",
       "      <td>2023-01-12</td>\n",
       "      <td>73.134776</td>\n",
       "      <td>1.668533</td>\n",
       "      <td>0.561717</td>\n",
       "      <td>-14.696779</td>\n",
       "      <td>18.049646</td>\n",
       "      <td>高速</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>270</td>\n",
       "      <td>2023-01-04</td>\n",
       "      <td>89.514422</td>\n",
       "      <td>-1.325766</td>\n",
       "      <td>0.699275</td>\n",
       "      <td>-18.843614</td>\n",
       "      <td>0.988950</td>\n",
       "      <td>市区</td>\n",
       "      <td>0</td>\n",
       "      <td>20</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>106</td>\n",
       "      <td>2023-01-30</td>\n",
       "      <td>64.221220</td>\n",
       "      <td>5.614003</td>\n",
       "      <td>0.299589</td>\n",
       "      <td>15.186566</td>\n",
       "      <td>17.039108</td>\n",
       "      <td>高速</td>\n",
       "      <td>1</td>\n",
       "      <td>22</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49995</th>\n",
       "      <td>381</td>\n",
       "      <td>2023-01-15</td>\n",
       "      <td>18.177682</td>\n",
       "      <td>-3.431543</td>\n",
       "      <td>0.591230</td>\n",
       "      <td>-14.562768</td>\n",
       "      <td>1.046809</td>\n",
       "      <td>高速</td>\n",
       "      <td>0</td>\n",
       "      <td>17</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49996</th>\n",
       "      <td>44</td>\n",
       "      <td>2023-01-07</td>\n",
       "      <td>120.000000</td>\n",
       "      <td>1.958547</td>\n",
       "      <td>0.861104</td>\n",
       "      <td>1.955887</td>\n",
       "      <td>0.141749</td>\n",
       "      <td>国道</td>\n",
       "      <td>0</td>\n",
       "      <td>23</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49997</th>\n",
       "      <td>916</td>\n",
       "      <td>2023-01-24</td>\n",
       "      <td>49.901630</td>\n",
       "      <td>0.306843</td>\n",
       "      <td>0.146259</td>\n",
       "      <td>-1.815499</td>\n",
       "      <td>2.017120</td>\n",
       "      <td>国道</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49998</th>\n",
       "      <td>171</td>\n",
       "      <td>2023-01-27</td>\n",
       "      <td>45.385097</td>\n",
       "      <td>-2.429129</td>\n",
       "      <td>0.370914</td>\n",
       "      <td>-5.591587</td>\n",
       "      <td>3.412434</td>\n",
       "      <td>高速</td>\n",
       "      <td>0</td>\n",
       "      <td>22</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49999</th>\n",
       "      <td>220</td>\n",
       "      <td>2023-01-02</td>\n",
       "      <td>81.593714</td>\n",
       "      <td>-0.612339</td>\n",
       "      <td>0.556499</td>\n",
       "      <td>7.657142</td>\n",
       "      <td>10.762482</td>\n",
       "      <td>市区</td>\n",
       "      <td>0</td>\n",
       "      <td>16</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>50000 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       vehicle_id  timestamp       speed  acceleration  braking_force  \\\n",
       "0             102 2023-01-01   75.472060     -0.465906       0.869633   \n",
       "1             435 2023-01-27   63.045652     -2.997739       0.261838   \n",
       "2             860 2023-01-12   73.134776      1.668533       0.561717   \n",
       "3             270 2023-01-04   89.514422     -1.325766       0.699275   \n",
       "4             106 2023-01-30   64.221220      5.614003       0.299589   \n",
       "...           ...        ...         ...           ...            ...   \n",
       "49995         381 2023-01-15   18.177682     -3.431543       0.591230   \n",
       "49996          44 2023-01-07  120.000000      1.958547       0.861104   \n",
       "49997         916 2023-01-24   49.901630      0.306843       0.146259   \n",
       "49998         171 2023-01-27   45.385097     -2.429129       0.370914   \n",
       "49999         220 2023-01-02   81.593714     -0.612339       0.556499   \n",
       "\n",
       "       steering_angle  continuous_driving location  night_driving  hour  \\\n",
       "0           11.094222            1.628275       市区              1    11   \n",
       "1            7.092074            8.379536       高速              1    13   \n",
       "2          -14.696779           18.049646       高速              0     5   \n",
       "3          -18.843614            0.988950       市区              0    20   \n",
       "4           15.186566           17.039108       高速              1    22   \n",
       "...               ...                 ...      ...            ...   ...   \n",
       "49995      -14.562768            1.046809       高速              0    17   \n",
       "49996        1.955887            0.141749       国道              0    23   \n",
       "49997       -1.815499            2.017120       国道              1    10   \n",
       "49998       -5.591587            3.412434       高速              0    22   \n",
       "49999        7.657142           10.762482       市区              0    16   \n",
       "\n",
       "       is_night  \n",
       "0             0  \n",
       "1             0  \n",
       "2             1  \n",
       "3             0  \n",
       "4             0  \n",
       "...         ...  \n",
       "49995         0  \n",
       "49996         1  \n",
       "49997         0  \n",
       "49998         0  \n",
       "49999         0  \n",
       "\n",
       "[50000 rows x 11 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a59aaf0d-8884-492a-9bd2-b9d2822d3dcb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "43d6eb1c-30af-4816-b60b-b54a9fcaa4e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ========== 驾驶行为分析 ==========\n",
    "def analyze_driving_behavior(df):\n",
    "    \"\"\"计算关键驾驶行为指标\"\"\"\n",
    "    # 风险行为标记\n",
    "    df['overspeed'] = np.where(df['speed'] > 90, 1, 0)  # 超速标记\n",
    "    df['hard_brake'] = np.where(df['braking_force'] > 0.8, 1, 0)  # 急刹车\n",
    "    df['sharp_turn'] = np.where(np.abs(df['steering_angle']) > 30, 1, 0)  # 急转弯\n",
    "    df['fatigue_driving'] = np.where(df['continuous_driving'] > 4, 1, 0)  # 疲劳驾驶\n",
    "    \n",
    "    # 聚合车辆级指标\n",
    "    vehicle_stats = df.groupby('vehicle_id').agg(\n",
    "        total_trips=('timestamp', 'count'),\n",
    "        overspeed_rate=('overspeed', 'mean'),\n",
    "        hard_brake_count=('hard_brake', 'sum'),\n",
    "        sharp_turn_count=('sharp_turn', 'sum'),\n",
    "        fatigue_duration=('continuous_driving', 'max'),\n",
    "        night_driving_ratio=('night_driving', 'mean')\n",
    "    ).reset_index()\n",
    "    \n",
    "    return vehicle_stats\n",
    "\n",
    "# 执行分析\n",
    "vehicle_behavior = analyze_driving_behavior(data).copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "435acac8-bfce-4121-a76f-748a2be4202e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>vehicle_id</th>\n",
       "      <th>total_trips</th>\n",
       "      <th>overspeed_rate</th>\n",
       "      <th>hard_brake_count</th>\n",
       "      <th>sharp_turn_count</th>\n",
       "      <th>fatigue_duration</th>\n",
       "      <th>night_driving_ratio</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>53</td>\n",
       "      <td>0.188679</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "      <td>14.150599</td>\n",
       "      <td>0.339623</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>37</td>\n",
       "      <td>0.243243</td>\n",
       "      <td>10</td>\n",
       "      <td>1</td>\n",
       "      <td>20.327907</td>\n",
       "      <td>0.324324</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>41</td>\n",
       "      <td>0.268293</td>\n",
       "      <td>7</td>\n",
       "      <td>3</td>\n",
       "      <td>14.052949</td>\n",
       "      <td>0.317073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>41</td>\n",
       "      <td>0.170732</td>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "      <td>23.266212</td>\n",
       "      <td>0.243902</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>44</td>\n",
       "      <td>0.295455</td>\n",
       "      <td>10</td>\n",
       "      <td>7</td>\n",
       "      <td>18.087835</td>\n",
       "      <td>0.340909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>995</td>\n",
       "      <td>51</td>\n",
       "      <td>0.176471</td>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>19.633470</td>\n",
       "      <td>0.235294</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>996</td>\n",
       "      <td>48</td>\n",
       "      <td>0.312500</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>26.663861</td>\n",
       "      <td>0.291667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>997</td>\n",
       "      <td>53</td>\n",
       "      <td>0.150943</td>\n",
       "      <td>16</td>\n",
       "      <td>6</td>\n",
       "      <td>19.490388</td>\n",
       "      <td>0.358491</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>998</td>\n",
       "      <td>51</td>\n",
       "      <td>0.274510</td>\n",
       "      <td>10</td>\n",
       "      <td>3</td>\n",
       "      <td>13.403434</td>\n",
       "      <td>0.196078</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>999</td>\n",
       "      <td>50</td>\n",
       "      <td>0.300000</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>12.457734</td>\n",
       "      <td>0.280000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     vehicle_id  total_trips  overspeed_rate  hard_brake_count  \\\n",
       "0             0           53        0.188679                12   \n",
       "1             1           37        0.243243                10   \n",
       "2             2           41        0.268293                 7   \n",
       "3             3           41        0.170732                 8   \n",
       "4             4           44        0.295455                10   \n",
       "..          ...          ...             ...               ...   \n",
       "995         995           51        0.176471                 7   \n",
       "996         996           48        0.312500                12   \n",
       "997         997           53        0.150943                16   \n",
       "998         998           51        0.274510                10   \n",
       "999         999           50        0.300000                12   \n",
       "\n",
       "     sharp_turn_count  fatigue_duration  night_driving_ratio  \n",
       "0                   0         14.150599             0.339623  \n",
       "1                   1         20.327907             0.324324  \n",
       "2                   3         14.052949             0.317073  \n",
       "3                   0         23.266212             0.243902  \n",
       "4                   7         18.087835             0.340909  \n",
       "..                ...               ...                  ...  \n",
       "995                 1         19.633470             0.235294  \n",
       "996                 1         26.663861             0.291667  \n",
       "997                 6         19.490388             0.358491  \n",
       "998                 3         13.403434             0.196078  \n",
       "999                 1         12.457734             0.280000  \n",
       "\n",
       "[1000 rows x 7 columns]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vehicle_behavior"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "ca1de889-2ce4-466c-ae23-ae2a4452cc4f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['vehicle_id',\n",
       " 'total_trips',\n",
       " 'overspeed_rate',\n",
       " 'hard_brake_count',\n",
       " 'sharp_turn_count',\n",
       " 'fatigue_duration',\n",
       " 'night_driving_ratio']"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vehicle_behavior.columns.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "54b6df7c-6d44-4980-a2a5-7f6a33485c61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "vehicle_id               int64\n",
       "total_trips              int64\n",
       "overspeed_rate         float64\n",
       "hard_brake_count         int64\n",
       "sharp_turn_count         int64\n",
       "fatigue_duration       float64\n",
       "night_driving_ratio    float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vehicle_behavior.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "ffc31ead-8c06-413e-a678-6cae932c0a47",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ========== 风险评级模型 ==========\n",
    "def risk_rating_model(df):\n",
    "    \"\"\"基于驾驶行为的风险分级模型\"\"\"\n",
    "    # 特征工程\n",
    "    df['risk_score'] = (\n",
    "        df['overspeed_rate'] * 0.4 +\n",
    "        df['hard_brake_count'] * 0.2 +\n",
    "        df['sharp_turn_count'] * 0.2 +\n",
    "        df['night_driving_ratio'] * 0.2\n",
    "    )\n",
    "    \n",
    "    # 风险分级 (A-D)\n",
    "    bins = [0, 0.15, 0.3, 0.45, float('inf')]\n",
    "    labels = ['A', 'B', 'C', 'D']\n",
    "    df['risk_rating'] = pd.cut(df['risk_score'], bins=bins, labels=labels)\n",
    "    \n",
    "    # 添加风险系数\n",
    "    risk_coefficient = {'A': 1.0, 'B': 1.2, 'C': 1.5, 'D': 2.0}\n",
    "    df['risk_coeff'] = df['risk_rating'].map(risk_coefficient)\n",
    "    \n",
    "    return df[['vehicle_id', 'risk_rating', 'risk_coeff', 'risk_score']]\n",
    "\n",
    "# 执行评级\n",
    "risk_ratings = risk_rating_model(vehicle_behavior.copy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f149c58c-e53e-45a0-9520-3bd84e3dd49a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>vehicle_id</th>\n",
       "      <th>risk_rating</th>\n",
       "      <th>risk_coeff</th>\n",
       "      <th>risk_score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>D</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.543396</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>D</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.362162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>D</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.170732</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>D</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.717073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>D</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.586364</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>995</td>\n",
       "      <td>D</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.717647</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>996</td>\n",
       "      <td>D</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.783333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>997</td>\n",
       "      <td>D</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.532075</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>998</td>\n",
       "      <td>D</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.749020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>999</td>\n",
       "      <td>D</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.776000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     vehicle_id risk_rating risk_coeff  risk_score\n",
       "0             0           D        2.0    2.543396\n",
       "1             1           D        2.0    2.362162\n",
       "2             2           D        2.0    2.170732\n",
       "3             3           D        2.0    1.717073\n",
       "4             4           D        2.0    3.586364\n",
       "..          ...         ...        ...         ...\n",
       "995         995           D        2.0    1.717647\n",
       "996         996           D        2.0    2.783333\n",
       "997         997           D        2.0    4.532075\n",
       "998         998           D        2.0    2.749020\n",
       "999         999           D        2.0    2.776000\n",
       "\n",
       "[1000 rows x 4 columns]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "risk_ratings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "4b082ae1-1916-4d59-bb9c-f7bf8410b2c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "vehicle_id        int64\n",
       "risk_rating    category\n",
       "risk_coeff     category\n",
       "risk_score      float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "risk_ratings.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "963a8326-e5dc-449d-bafd-54758db65be6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ========== UBI保险模型 ==========\n",
    "def ubi_insurance_model(behavior_df, risk_df):\n",
    "    \"\"\"UBI保险定价与干预模型\"\"\"\n",
    "    # 合并数据\n",
    "    df = pd.merge(behavior_df, risk_df, on='vehicle_id')\n",
    "    \n",
    "    # 基础保费计算\n",
    "    base_premium = 5000  # 元/年\n",
    "    \n",
    "    # 风险调整保费\n",
    "    df['adjusted_premium'] = base_premium * df['risk_coeff'].astype(np.float32)\n",
    "    \n",
    "    # 高风险车辆识别\n",
    "    df['high_risk'] = np.where(df['risk_rating'].isin(['C', 'D']), 1, 0)\n",
    "    \n",
    "    # 模拟干预效果：降低高风险行为5%\n",
    "    intervention_effect = 0.05\n",
    "    df['post_intervention_score'] = np.where(\n",
    "        df['high_risk'] == 1,\n",
    "        df['risk_score'] * (1 - intervention_effect),\n",
    "        df['risk_score']\n",
    "    )\n",
    "    \n",
    "    return df\n",
    "\n",
    "# 执行UBI模型  vehicle_behavior\n",
    "insurance_data = ubi_insurance_model(vehicle_behavior, risk_ratings)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "3d5baaa8-35e2-40c2-9b27-55a421ab07c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>vehicle_id</th>\n",
       "      <th>total_trips</th>\n",
       "      <th>overspeed_rate</th>\n",
       "      <th>hard_brake_count</th>\n",
       "      <th>sharp_turn_count</th>\n",
       "      <th>fatigue_duration</th>\n",
       "      <th>night_driving_ratio</th>\n",
       "      <th>risk_rating</th>\n",
       "      <th>risk_coeff</th>\n",
       "      <th>risk_score</th>\n",
       "      <th>adjusted_premium</th>\n",
       "      <th>high_risk</th>\n",
       "      <th>post_intervention_score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>53</td>\n",
       "      <td>0.188679</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "      <td>14.150599</td>\n",
       "      <td>0.339623</td>\n",
       "      <td>D</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.543396</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.416226</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>37</td>\n",
       "      <td>0.243243</td>\n",
       "      <td>10</td>\n",
       "      <td>1</td>\n",
       "      <td>20.327907</td>\n",
       "      <td>0.324324</td>\n",
       "      <td>D</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.362162</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.244054</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>41</td>\n",
       "      <td>0.268293</td>\n",
       "      <td>7</td>\n",
       "      <td>3</td>\n",
       "      <td>14.052949</td>\n",
       "      <td>0.317073</td>\n",
       "      <td>D</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.170732</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.062195</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>41</td>\n",
       "      <td>0.170732</td>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "      <td>23.266212</td>\n",
       "      <td>0.243902</td>\n",
       "      <td>D</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.717073</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.631220</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>44</td>\n",
       "      <td>0.295455</td>\n",
       "      <td>10</td>\n",
       "      <td>7</td>\n",
       "      <td>18.087835</td>\n",
       "      <td>0.340909</td>\n",
       "      <td>D</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.586364</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.407045</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>995</td>\n",
       "      <td>51</td>\n",
       "      <td>0.176471</td>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>19.633470</td>\n",
       "      <td>0.235294</td>\n",
       "      <td>D</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.717647</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.631765</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>996</td>\n",
       "      <td>48</td>\n",
       "      <td>0.312500</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>26.663861</td>\n",
       "      <td>0.291667</td>\n",
       "      <td>D</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.783333</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.644167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>997</td>\n",
       "      <td>53</td>\n",
       "      <td>0.150943</td>\n",
       "      <td>16</td>\n",
       "      <td>6</td>\n",
       "      <td>19.490388</td>\n",
       "      <td>0.358491</td>\n",
       "      <td>D</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.532075</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4.305472</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>998</td>\n",
       "      <td>51</td>\n",
       "      <td>0.274510</td>\n",
       "      <td>10</td>\n",
       "      <td>3</td>\n",
       "      <td>13.403434</td>\n",
       "      <td>0.196078</td>\n",
       "      <td>D</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.749020</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.611569</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>999</td>\n",
       "      <td>50</td>\n",
       "      <td>0.300000</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>12.457734</td>\n",
       "      <td>0.280000</td>\n",
       "      <td>D</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.776000</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.637200</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 13 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     vehicle_id  total_trips  overspeed_rate  hard_brake_count  \\\n",
       "0             0           53        0.188679                12   \n",
       "1             1           37        0.243243                10   \n",
       "2             2           41        0.268293                 7   \n",
       "3             3           41        0.170732                 8   \n",
       "4             4           44        0.295455                10   \n",
       "..          ...          ...             ...               ...   \n",
       "995         995           51        0.176471                 7   \n",
       "996         996           48        0.312500                12   \n",
       "997         997           53        0.150943                16   \n",
       "998         998           51        0.274510                10   \n",
       "999         999           50        0.300000                12   \n",
       "\n",
       "     sharp_turn_count  fatigue_duration  night_driving_ratio risk_rating  \\\n",
       "0                   0         14.150599             0.339623           D   \n",
       "1                   1         20.327907             0.324324           D   \n",
       "2                   3         14.052949             0.317073           D   \n",
       "3                   0         23.266212             0.243902           D   \n",
       "4                   7         18.087835             0.340909           D   \n",
       "..                ...               ...                  ...         ...   \n",
       "995                 1         19.633470             0.235294           D   \n",
       "996                 1         26.663861             0.291667           D   \n",
       "997                 6         19.490388             0.358491           D   \n",
       "998                 3         13.403434             0.196078           D   \n",
       "999                 1         12.457734             0.280000           D   \n",
       "\n",
       "    risk_coeff  risk_score  adjusted_premium  high_risk  \\\n",
       "0          2.0    2.543396           10000.0          1   \n",
       "1          2.0    2.362162           10000.0          1   \n",
       "2          2.0    2.170732           10000.0          1   \n",
       "3          2.0    1.717073           10000.0          1   \n",
       "4          2.0    3.586364           10000.0          1   \n",
       "..         ...         ...               ...        ...   \n",
       "995        2.0    1.717647           10000.0          1   \n",
       "996        2.0    2.783333           10000.0          1   \n",
       "997        2.0    4.532075           10000.0          1   \n",
       "998        2.0    2.749020           10000.0          1   \n",
       "999        2.0    2.776000           10000.0          1   \n",
       "\n",
       "     post_intervention_score  \n",
       "0                   2.416226  \n",
       "1                   2.244054  \n",
       "2                   2.062195  \n",
       "3                   1.631220  \n",
       "4                   3.407045  \n",
       "..                       ...  \n",
       "995                 1.631765  \n",
       "996                 2.644167  \n",
       "997                 4.305472  \n",
       "998                 2.611569  \n",
       "999                 2.637200  \n",
       "\n",
       "[1000 rows x 13 columns]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "insurance_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0a57c08-b9ad-4c2a-9676-a91bd0d59891",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "c4c44f7b-f57f-472f-b3a2-f40a8a794a66",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "⛑️ 风险分布:\n",
      "risk_rating\n",
      "D    1.0\n",
      "A    0.0\n",
      "B    0.0\n",
      "C    0.0\n",
      "Name: proportion, dtype: float64\n",
      "\n",
      "💰 保费统计:\n",
      "              count     mean  std      min      25%      50%      75%      max\n",
      "risk_rating                                                                   \n",
      "A               0.0      NaN  NaN      NaN      NaN      NaN      NaN      NaN\n",
      "B               0.0      NaN  NaN      NaN      NaN      NaN      NaN      NaN\n",
      "C               0.0      NaN  NaN      NaN      NaN      NaN      NaN      NaN\n",
      "D            1000.0  10000.0  0.0  10000.0  10000.0  10000.0  10000.0  10000.0\n",
      "\n",
      "📉 干预后风险评分下降: 5.00%\n",
      "💡 预计赔付率降低: 4.00% (基于历史数据模型)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_206182/1978086700.py:8: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n",
      "  premium_stats = df.groupby('risk_rating')['adjusted_premium'].describe()\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# ========== 结果分析 ==========\n",
    "def analyze_results(df):\n",
    "    \"\"\"业务效果分析\"\"\"\n",
    "    # 风险分布统计\n",
    "    risk_dist = df['risk_rating'].value_counts(normalize=True)\n",
    "    \n",
    "    # 保费变化分析\n",
    "    premium_stats = df.groupby('risk_rating')['adjusted_premium'].describe()\n",
    "    \n",
    "    # 干预效果对比\n",
    "    original_risk = df['risk_score'].mean()\n",
    "    improved_risk = df['post_intervention_score'].mean()\n",
    "    reduction = (original_risk - improved_risk) / original_risk\n",
    "    \n",
    "    print(f\"⛑️ 风险分布:\\n{risk_dist}\\n\")\n",
    "    print(f\"💰 保费统计:\\n{premium_stats}\\n\")\n",
    "    print(f\"📉 干预后风险评分下降: {reduction:.2%}\")\n",
    "    print(f\"💡 预计赔付率降低: {reduction*0.8:.2%} (基于历史数据模型)\")\n",
    "\n",
    "# 输出业务效果\n",
    "analyze_results(insurance_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5478dd45-048f-49a4-ae80-5398cfd2c2c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "analyze_results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51e005d7-69c6-4043-b5f3-551de7a6ad6a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc15e2ac-f9fc-467d-bd56-08d5a20a81f3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2acf83f-891d-4012-99af-b3e0024f58d4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78984141-ec94-4f4a-accd-12a3c63446bd",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14b76bd4-1782-4027-a9e8-6648f88dd544",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26ca6037-1460-4a19-bb3e-3f51abcb4f47",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e53f8abf-a274-44ed-923c-f8ee47b9065c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   id     name  salary\n",
      "0   2      Bob   50000\n",
      "1   3  Charlie   60000\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df1 = pd.DataFrame({\n",
    "    'id': [1, 2, 3],\n",
    "    'name': ['Alice', 'Bob', 'Charlie']\n",
    "})\n",
    "\n",
    "df2 = pd.DataFrame({\n",
    "    'id': [2, 3, 4],\n",
    "    'salary': [50000, 60000, 70000]\n",
    "})\n",
    "\n",
    "# 内连接：仅保留两表共有的 id\n",
    "result = pd.merge(df1, df2, on='id')  # 同名列自动匹配（on 参数可省略）\n",
    "print(result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61cd2c05-c700-4ab7-8683-3961e72a2bc1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "407d97e7-dfc2-4929-83ba-7ceaecd40f46",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
